package com.bxd.baodan.util;

import com.j256.ormlite.dao.Dao;
import com.bxd.baodan.App;
import com.bxd.baodan.app.Constant;
import com.bxd.baodan.db.DBHelper;
import com.bxd.baodan.item.MessageItem;
import com.tencent.TIMConversation;
import com.tencent.TIMManager;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by lizhichao on 18/1/24.
 */

public class MsgUtil {
    public static boolean getImUnread() {
        boolean hasUnRead = false;
        for (TIMConversation con : TIMManager.getInstance().getConversionList()) {

            String planer = SpUtil.getInstance().getString("planerId");
            if (!StringUtil.isEmpty(planer)) {
                String[] planerIds = planer.split(",");
                for (String planerId : planerIds) {

                    if (con.getIdentifer().equals(planerId) || con.getPeer().equals(planerId)) {
                        long num = con.getUnreadMessageNum();

                        if (num > 0) {
                            hasUnRead = true;
                            break;
                        }
                    }
                }
            }

        }
        return hasUnRead;
    }


    public static void setImReaded() {
        for (TIMConversation con : TIMManager.getInstance().getConversionList()) {

            String planer = SpUtil.getInstance().getString("planerId");
            if (!StringUtil.isEmpty(planer)) {
                String[] planerIds = planer.split(",");
                for (String planerId : planerIds) {

                    if (con.getIdentifer().equals(planerId) || con.getPeer().equals(planerId)) {
                        con.setReadMessage();
                    }
                }
            }

        }

    }


    public static boolean getPushMsg() {

        Dao<MessageItem, ?> dao = null;
        if (dao == null) {
            try {
                dao = DBHelper.getInstance(App.getContext()).getDao(MessageItem.class);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


        long push_msg = 0;

        try {
            push_msg = dao.queryBuilder().where().eq("isread", false).and().in("type",
                    Constant.MSG_NEW_ACTIVITIES,
                    Constant.MSG_NEW_READ, Constant.MSG_NEW_BAODAN
                    , Constant.MSG_NEW_ANSWER,
                    Constant.MSG_NEW_ANSWER_ADD).countOf();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return push_msg > 0;


    }


    public static boolean getSysPushMsg() {

        Dao<MessageItem, ?> dao = null;
        if (dao == null) {
            try {
                dao = DBHelper.getInstance(App.getContext()).getDao(MessageItem.class);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


        long push_msg = 0;

        try {
            push_msg = dao.queryBuilder().where().eq("isread", false).and().in("type",
                    Constant.MSG_NEW_ACTIVITIES,
                    Constant.MSG_NEW_READ, Constant.MSG_NEW_BAODAN
            ).countOf();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return push_msg > 0;


    }

    public static boolean getAnsPushMsg() {

        Dao<MessageItem, ?> dao = null;
        if (dao == null) {
            try {
                dao = DBHelper.getInstance(App.getContext()).getDao(MessageItem.class);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


        long push_msg = 0;

        try {
            push_msg = dao.queryBuilder().where().eq("isread", false).and().in("type",
                    Constant.MSG_NEW_ANSWER,
                    Constant.MSG_NEW_ANSWER_ADD).countOf();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return push_msg > 0;


    }

    public static void allPushRead() {
        Dao<MessageItem, ?> dao = null;
        List<MessageItem> list;
        try {
            dao = DBHelper.getInstance(App.getContext()).getDao(MessageItem.class);
            list = dao.queryBuilder().where().eq("isread", false).query();
            for (MessageItem item : list) {
                item.setIsread(true);
                // FIXME: 18/1/26 修复全部消息已读,未刷新
                dao.createOrUpdate(item);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

}
